<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/26
 * Time: 下午6:16
 */

namespace app\api\service;


use app\api\model\WgArea as WgAreaModel;
use app\api\model\WgDevice as WgDeviceModel;

class WgService extends Base
{
    public function areaCreate($guid,$data)
    {
        $where = [
            'name' => $data['name']
        ];
        $OrgInfo = $this->getAreaInfo($where,['name']);
        //添加检查
        if (!is_null($OrgInfo))
            set_error_exception('wg_area_name_exists');
        //检查结束
        $row = WgAreaModel::addInfo($guid,$data)->id;
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function getAreaInfo($where,$field=true)
    {
        $infoObj = WgAreaModel::getInfo($where,$field);
        is_null($infoObj) ? : $infoObj = $infoObj->toArray();
        return $infoObj;
    }

    public function areaUpdate($guid,$where,$data)
    {
        //数据不为空检查
        $field = $this->checkParamIsNull($data);

        $odata = $this->getAreaInfo($where,$field);
        //数据不存在检查
        if (empty($odata)) {
            set_error_exception('data_not_exists');
        }
        //数据无修改检查
        foreach ($data as $k => $v) {
            if ($v === $odata[$k]) {
                set_error_exception('error',['code'=>10001,'msg'=>$k.'无修改']);
            }
        }
        //数据唯一检查
        $unqiue = ['name'];
        foreach ($unqiue as $k => $v) {
            if (isset($data[$v])) {
                $map = [
                    $v => $data[$v]
                ];
                if(!empty($this->getAreaInfo($map,['id'])))
                    set_error_exception('error',['code'=>10001,'msg'=>$v.'重复']);
            }
        }

        $row = WgAreaModel::updateInfo($guid,$odata,$data,$where);
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function getWgAreaInfoListByMap($where,$page)
    {
        $result = $list = [];
        if (!empty($page)) {
            $count = WgAreaModel::where($where)->count();
            $list_max_rows = config('list_rows_20');
            $total_page = ceil($count/$list_max_rows);
            $current_page = (int)$page;
            if ($count > 0) {
                $obj = WgAreaModel::getInfoList($where,['id','name','address','area_head'],$page);
                foreach ($obj as $k => $v) {
                    $list[] = $v->toArray();
                }
            }
            $result = [
                'count' => $count,
                'list_max_rows' => $list_max_rows,
                'list_rows' => count($list),
                'total_page' => $total_page,
                'current_page' => $current_page,
                'list' => $list
            ];
        } else {
            $obj = WgAreaModel::getInfoList($where,['id','name','address','area_head']);
            foreach ($obj as $k => $v) {
                $result[] = $v->toArray();
            }
        }
        return $result;
    }

    public function deviceCreate($guid,$data)
    {
        $where = [
            'code' => $data['code']
        ];
        $OrgInfo = $this->getDeviceInfo($where,['code']);
        //添加检查
        if (!is_null($OrgInfo))
            set_error_exception('wg_device_name_exists');
        //检查结束
        $row = WgDeviceModel::addInfo($guid,$data)->id;
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function getDeviceInfo($where,$field=true)
    {
        $infoObj = WgDeviceModel::getInfo($where,$field);
        is_null($infoObj) ? : $infoObj = $infoObj->toArray();
        return $infoObj;
    }

    public function deviceUpdate($guid,$where,$data)
    {
        //数据不为空检查
        $field = $this->checkParamIsNull($data);

        $odata = $this->getDeviceInfo($where,$field);
        //数据不存在检查
        if (empty($odata)) {
            set_error_exception('data_not_exists');
        }
        //数据无修改检查
        foreach ($data as $k => $v) {
            if ($v === $odata[$k]) {
                set_error_exception('error',['code'=>10001,'msg'=>$k.'无修改']);
            }
        }
        //数据唯一检查
        $unqiue = ['code'];
        foreach ($unqiue as $k => $v) {
            if (isset($data[$v])) {
                $map = [
                    $v => $data[$v]
                ];
                if(!empty($this->getDeviceInfo($map,['id'])))
                    set_error_exception('error',['code'=>10001,'msg'=>$v.'重复']);
            }
        }

        $row = WgDeviceModel::updateInfo($guid,$odata,$data,$where);
        $result = $this->dbOperationResult($row);
        return $result;
    }

    public function getWgdeviceInfoListByMap($where,$page)
    {
        $result = $list = [];
        if (!empty($page)) {
            $count = WgDeviceModel::where($where)->count();
            $list_max_rows = config('list_rows_20');
            $total_page = ceil($count/$list_max_rows);
            $current_page = (int)$page;
            if ($count > 0) {
                $obj = WgDeviceModel::getInfoList($where,['id','code','guid','area_id'],$page,['wg_area','user']);
                foreach ($obj as $k => $v) {
                    $list[] = $v->toArray();
                }
            }
            $result = [
                'count' => $count,
                'list_max_rows' => $list_max_rows,
                'list_rows' => count($list),
                'total_page' => $total_page,
                'current_page' => $current_page,
                'list' => $list
            ];
        } else {
            $obj = WgDeviceModel::getInfoList($where,['id','code','guid','area_id'],null,['wg_area','user']);
            foreach ($obj as $k => $v) {
                $result[] = $v->toArray();
            }
        }
        return $result;
    }
}